import Patch from './patch.mdx'
import UniappCliStyle from './UniappCliStyle.mdx'

# uni-app cli vue2 webpack

## 安装

```bash npm2yarn
npm install -D tailwindcss @tailwindcss/postcss postcss weapp-tailwindcss
```

<Patch />

## 配置

### 创建 `vue.config.js`

```js title="vue.config.js"
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss/webpack')
const path = require('node:path')
/**
 * @type {import('@vue/cli-service').ProjectOptions}
 */
const config = {
  // some option...
  configureWebpack: (config) => {
    config.plugins.push(
      new UnifiedWebpackPluginV5({
        rem2rpx: true,
        cssEntries: [
          // 你 @import "weapp-tailwindcss"; 那个文件绝对路径
          path.resolve(__dirname, './src/app.css'),
        ],
      })
    )
  }
  // other option...
}

module.exports = config
```

> tailwindcss@4 必须配置 `cssEntries` 并且使用绝对路径，否则 `tailwindcss` 生成的类名不会参与转译。

## 配置 `postcss.config.js`

```js title="postcss.config.js"
const path = require('path')
const webpack = require('webpack')
const config = {
  parser: require('postcss-comment'),
  plugins: [
    // highlight-next-line
    require('@tailwindcss/postcss')(), // 只添加这一行
    require('postcss-import')({
      resolve (id, basedir, importOptions) {
        if (id.startsWith('~@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3))
        } else if (id.startsWith('@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2))
        } else if (id.startsWith('/') && !id.startsWith('//')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1))
        }
        return id
      }
    }),

    require('autoprefixer')({
      remove: process.env.UNI_PLATFORM !== 'h5'
    }),
    require('@dcloudio/vue-cli-plugin-uni/packages/postcss')
  ]
}
if (webpack.version[0] > 4) {
  delete config.parser
}
module.exports = config
```

## 添加样式

<UniappCliStyle />

然后直接运行到小程序，微信开发者工具导入这个项目，即可看到效果

## 参考模板

https://github.com/icebreaker-template/uni-app-webpack-tailwindcss-v4
